
package org.movee.spring.data.hugegraph.repository.factory;

import org.movee.spring.data.hugegraph.query.HugeOperations;
import org.movee.spring.data.hugegraph.query.HugeTemplate;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.util.Assert;

import java.io.Serializable;

public class HugeRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
        extends RepositoryFactoryBeanSupport<T, S, ID> {

    private HugeOperations operations;

    /**
     * Create a new {@link HugeRepositoryFactoryBean} for the given repository interface.
     *
     * @param repositoryInterface must not be {@literal null}.
     */
    public HugeRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
        super(repositoryInterface);

    }

    /**
     * 把 HugeTemplate 注入进来
     * @param hugeTemplate
     */
    public void setHugeTemplate(final HugeTemplate hugeTemplate) {

        this.operations = hugeTemplate;
        setMappingContext(hugeTemplate.getConverter().getMappingContext());
    }

    @Override
    protected final RepositoryFactorySupport createRepositoryFactory() {

        Assert.state(operations != null, "GremlinOperations must not be null");

        return new HugeRepositoryFactory(operations);
    }


    @Override
    public void afterPropertiesSet() {
        super.afterPropertiesSet();

        Assert.notNull(operations, "GremlinOperations must not be null!");

    }
}
